package xkp.lesson1;

public class Testyield {
    public static void main(String[] args) {
        Thread thread = new MyThread();
        //调用start方法之后，线程从新建状态转换为Runnable(就绪态)
        //等待被操作系统调度
        thread.start();
        //线程礼让
        //此时改行代码所在的线程立马打回Runnable(就绪态)
        Thread.yield();
        //该输出经常会跑到线程前面
        for(int i = 1; i <= 10; i++){
            System.out.println(i+":"+Thread.currentThread().getName());
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class MyThread extends Thread{
    @Override
    public void run() {
        for(int i = 1; i <= 10; i++){
            System.out.println(i+":"+Thread.currentThread().getName());
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
